package com.hnkywl.sts.action.cggl;

import com.google.gson.Gson;
import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.cggl.Cgdmx;
import com.hnkywl.sts.entity.cggl.search.QueryCgd;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.cggl.NbcgdService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.EasyuiAdapter;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-7-7
 * Time: 下午3:10
 * To change this template use File | Settings | File Templates.
 */

@Namespace("/cggl/nbcgd")
public class NbcgdAction extends SimpleActionSupport {
    //发货开始时间
    private Date fhrqStart;
    //发货结束时间
    private Date fhrqEnd;
    //采购结算单ID
    private Long cgjsdId;
    //采购单号
    private String cgdh;
    //页面当前位置
    private int page;
    //每页行数
    private int rows;
    //行索引
    private int index;
    private Long khId;

    private Long cggsId;

    private Long cgbmId;

    private Long cgrId;

    private Long gcId;
    private Long cghtId;

    private Long gysid;

    private String zt;

    private Date sysdate;

    private Date datetime;

    private String ewmtime;

    private Cgd cgd;

    private GsjgService gsjgService;
    //查询
    private QueryCgd queryCgd;

    private NbcgdService nbcgdService;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    private Long cgdId;

    private Double ce;

    private String cglx;

    private Long cgdwId;

    private String sftzd;
    private Yg yg;
    private String zdr = YgService.getCurrentYgName();
    private Date CurrentTime = new Date();
    private String htType;

    @Action("nbcgd_add")
    public void cgdAdd() {
        try {
            this.nbcgdService.add(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"保存采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cgd_stock")
    public void cgdStock() {
        try {
            this.nbcgdService.stock(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"入库成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cgd_del")
    public void cgdDel() {
        try {
            this.nbcgdService.delete(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cgd_confirm")
    public void cgdConfirm() {
        try {
            this.nbcgdService.confirm(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"提交采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cgd_cancel")
    public void cgdCancel() {
        try {
            this.nbcgdService.cancel(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"提交采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 采购单列表
     */
    @Action("cgdlist")
    public void xsdList() throws ParseException {
        if (cgdwId != null) {
            Gsjg g = this.gsjgService.findGsjgDetail(cgdwId);
            if (g instanceof Nbgs) {
                cggsId = cgdwId;
            } else {
                cgbmId = cgdwId;
            }
            this.renderJson(this.nbcgdService.findAllCgdList(cgdh, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd,htType, page, rows));
        } else {
            this.renderJson(this.nbcgdService.findAllCgdList(cgdh, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType,page, rows));
        }
    }

    @Action("edit_cgdtz")
    public void editCgdtz() {
        try {
            Cgd ycgd = this.nbcgdService.findCgdDetail(cgd.getId());
            initCgd(cgd);
            Cgd hccgd = new Cgd();
            ReflectionUtils.getInstance(hccgd, ycgd);
            initCgd(hccgd);
            this.nbcgdService.editCgdtz(cgd, hccgd, ycgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"调整采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    private void initCgd(Cgd cgd) {
        if (cgd.getSbgs() != null && cgd.getSbgs().getId() == null) {
            cgd.setSbgs(null);
        }
        if (cgd.getSbbm() != null && cgd.getSbbm().getId() == null) {
            cgd.setSbbm(null);
        }

        if (cgd.getSbr() != null && cgd.getSbr().getId() == null) {
            cgd.setSbr(null);
        }
        if (cgd.getFhgs() != null && cgd.getFhgs().getId() == null) {
            cgd.setFhgs(null);
        }
        if (cgd.getCght() != null && cgd.getCght().getId() == null) {
            cgd.setCght(null);
        }
        if (cgd.getDzck() != null && cgd.getDzck().getId() == null) {
            cgd.setDzck(null);
        }
        if (cgd.getCggs() != null && cgd.getCggs().getId() == null) {
            cgd.setCggs(null);
        }
        if (cgd.getCgbm() != null && cgd.getCgbm().getId() == null) {
            cgd.setCgbm(null);
        }
        if (cgd.getCgr() != null && cgd.getCgr().getId() == null) {
            cgd.setCgr(null);
        }

        if (cgd.getXsd() != null && cgd.getXsd().getId() == null) {
            cgd.setXsd(null);
        }

        if (cgd.getCgdmxList() != null) {
            for (Cgdmx cgdmx : cgd.getCgdmxList()) {
                if (cgdmx.getJzfs() != null && cgdmx.getJzfs().getId() == null) {
                    cgdmx.setJzfs(null);
                }
                if (cgdmx.getKh() != null && cgdmx.getKh().getId() == null) {
                    cgdmx.setKh(null);
                }
            }
        }
    }

    /**
     * 采购记录记录
     *
     * @return
     */
    @Action("cgjlList")
    public void cgjlList() {
        String[] propertyNames = {"id", "cgdh", "fhrq", "cglx", "gys.qc", "sbgs.qc", "sbbm.qc", "sbr.xm", "zt"};
        Page<Cgd> cgds = nbcgdService.findCgjlQuery(cghtId, page, rows);
        this.renderJson(EasyuiAdapter.getJsonForDataGrid(cgds, propertyNames));
    }

    @Action("nbcgd")
    public String nbcgd() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    @Action("cgdtz")
    public String cgdtz() {
        CurrentTime = new Date();
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    /**
     * 采购单
     */
    @Action("nbcgd_detail")
    public String cgddetail() {
        cgd = this.nbcgdService.findById(cgd.getId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        ewmtime = simpleDateFormat.format(cgd.getLastUpdated());
        return SUCCESS;
    }

    @Action("nbcgd_content")
    public String cgdcontent() {
        yg = YgService.getCurrentYg();
        if (cgd != null && cgd.getId() != null) {
            cgd = this.nbcgdService.findById(cgd.getId());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            ewmtime = simpleDateFormat.format(cgd.getLastUpdated());
        } else {
            sysdate = new Date(); //当前日期
            Calendar calendar = Calendar.getInstance();//日历对象
            calendar.setTime(sysdate);//设置当前日期
            calendar.add(Calendar.MONTH, 1);//将当前日期加一个月
            datetime = calendar.getTime();//输出格式化的日期
        }
        return SUCCESS;
    }

    @Action("nbcgd_stock")
    public String cgdstock() {
        cgd = this.nbcgdService.findById(cgd.getId());
        return SUCCESS;
    }

    @Action("nbcgd_cgmx")
    public void nbcgdCgmx() {
        Gson gson = new Gson();
        List<Map> cgmxs = this.nbcgdService.findCgmx(cgd.getId());
        Integer js = 0;
        Double sbsl = 0d;
        Double hsj = 0d;
        Double bhsj = 0d;
        Integer rkjs = 0;
        Double rksl = 0d;
        Double rkjz = 0d;
        String result = "";
        if (cgmxs.size() > 0) {
            for (Map map : cgmxs) {
                js += map.get("js") == null ? 0 : (Integer) map.get("js");
                sbsl = sbsl + (map.get("sbsl") == null ? 0d : Double.parseDouble(map.get("sbsl").toString()));
                hsj = hsj + (map.get("hsj") == null ? 0d : Double.parseDouble(map.get("hsj").toString()));
                bhsj = bhsj + (map.get("bhsj") == null ? 0d : Double.parseDouble(map.get("bhsj").toString()));
                rkjs = rkjs + (map.get("rkjs") == null ? 0 : Integer.parseInt(map.get("rkjs").toString()));
                rksl = rksl + (map.get("rksl") == null ? 0d : Double.parseDouble(map.get("rksl").toString()));
                rkjz = rkjz + (map.get("rkjz") == null ? 0d : Double.parseDouble(map.get("rkjz").toString()));
            }
            result = "{\"rows\":" + gson.toJson(cgmxs) + ",\"footer\":[{\"cd.id\":\"合计:\",\"js\":" + js
                    + ",\"sbsl\":" + sbsl + ",\"hsj\":" + hsj + ",\"bhsj\":" + bhsj + ",\"rkjs\":" + rkjs + ",\"rksl\":" + rksl + ",\"rkjz\":" + rkjz + "}]}";
        } else {
            result = "{\"rows\":" + gson.toJson(cgmxs) + "}]}";
        }
        this.renderJson(result);
    }

    /**
     *采购结算调用查询采购单明细
     */
    @Action("cgjsdSearch_cgdmx")
    public void cgjsdSreachCgmx() {
        Gson gson = new Gson();
        List<Map> cgmxs = this.nbcgdService.findCgjsdSearchCgmx(cgd.getId());
        Integer js = 0;
        Double sbsl = 0d;
        Double hsj = 0d;
        Double bhsj = 0d;
        Integer rkjs = 0;
        Double rksl = 0d;
        Double rkjz = 0d;
        String result = "";
        if (cgmxs.size() > 0) {
            for (Map map : cgmxs) {
                js += map.get("js") == null ? 0 : (Integer) map.get("js");
                sbsl = sbsl + (map.get("sbsl") == null ? 0d : Double.parseDouble(map.get("sbsl").toString()));
                hsj = hsj + (map.get("hsj") == null ? 0d : Double.parseDouble(map.get("hsj").toString()));
                bhsj = bhsj + (map.get("bhsj") == null ? 0d : Double.parseDouble(map.get("bhsj").toString()));
                rkjs = rkjs + (map.get("rkjs") == null ? 0 : Integer.parseInt(map.get("rkjs").toString()));
                rksl = rksl + (map.get("rksl") == null ? 0d : Double.parseDouble(map.get("rksl").toString()));
                rkjz = rkjz + (map.get("rkjz") == null ? 0d : Double.parseDouble(map.get("rkjz").toString()));
            }
            result = "{\"rows\":" + gson.toJson(cgmxs) + ",\"footer\":[{\"cd.id\":\"合计:\",\"js\":" + js
                    + ",\"sbsl\":" + sbsl + ",\"hsj\":" + hsj + ",\"bhsj\":" + bhsj + ",\"rkjs\":" + rkjs + ",\"rksl\":" + rksl + ",\"rkjz\":" + rkjz + "}]}";
        } else {
            result = "{\"rows\":" + gson.toJson(cgmxs) + "}]}";
        }
        this.renderJson(result);
    }


    @Action("nbcgd_cgfy")
    public void nbcgdCgfy() {
        Gson gson = new Gson();
        String result = StringUtils.EMPTY;
        Double jfsl = 0d;
        Double je = 0d;
        List<Map> cgfys = this.nbcgdService.findCgfy(cgd.getId());
        if (cgfys.size() > 0) {
            for (Map map : cgfys) {
                jfsl += map.get("jfsl") == null ? 0d : (Double) map.get("jfsl");
                je += map.get("je") == null ? 0d : (Double) map.get("je");
            }
            result = "{\"rows\":" + gson.toJson(this.nbcgdService.findCgfy(cgd.getId())) + ",\"footer\":[{\"fylb.fykmjc\":\"合计:\",\"jfsl\":" + jfsl
                    + ",\"je\":" + je + "}]}";
        } else {
            result = "{\"rows\":" + gson.toJson(this.nbcgdService.findCgfy(cgd.getId())) + "}";
        }
        this.renderJson(result);
    }

    @Action("cgdmxByCgdId")
    public void cgdmxByCgdId(){
        this.renderJson(this.nbcgdService.findCgdmxByCgdId(cgdId, ce));
    }

    //采购结算单中查询采购单
    @Action("cgdSearchList")
    public void cgdSearchList() {
        this.renderJson(this.nbcgdService.findCgdSearch(cgdh, fhrqStart, fhrqEnd, cgjsdId, cggsId, gysid, this.page, this.rows));
    }

    //采购结算单中查询采购单
    @Action("cgdmxSearchList")
    public void cgdmxSearchList() {
        this.renderJson(this.nbcgdService.findCgdmxSearch(cgjsdId));
    }

    @Action("cgdGysList")
    public void cgdGysList() {
        this.renderJson(this.nbcgdService.findGysByCgd(cggsId));
    }


    @Action("cgdDetail")
    public void cgdDetail() {
        Gson gson = new Gson();
        List<Map> cgdmxList = this.nbcgdService.findCgmx(cgdId);
        Double sbsl = 0d;
        Double hsj = 0d;
        Double bhsj = 0d;
        Integer rkjs = 0;
        Double rksl = 0d;
        Double yshje = 0d;
        String result = "";
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.00");
        StringBuffer sbTable = new StringBuffer();
        if (null != cgdmxList) {
            sbTable.append("<table class=\"tb intb\">");
            sbTable.append("<tr>");
            sbTable.append("<td style='width:100px;'>物资编码</td><td>品名</td><td>规格型号</td><td>图号/材质</td><td>产地</td>" +
                    "<td>单位</td><td>应送货数量</td><td>已送货数量</td><td>单价(含税)</td><td>总金额(含税)</td>");
            sbTable.append("</tr>");

            for (int i = 0; i < cgdmxList.size(); i++) {
                sbTable.append("<tr>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.code")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.code"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.name")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.name"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.ggxh")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.ggxh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("th")) {
                    sbTable.append(cgdmxList.get(i).get("th"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("cd.z")) {
                    sbTable.append(cgdmxList.get(i).get("cd.z"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("wzgg.dw")) {
                    sbTable.append(cgdmxList.get(i).get("wzgg.dw"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("sbsl")) {
                    sbTable.append(df.format(cgdmxList.get(i).get("sbsl")));
                    sbsl += (Double) (cgdmxList.get(i).get("sbsl"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("rksl")) {
                    sbTable.append(df.format(cgdmxList.get(i).get("rksl")));
                    rksl += (Double) (cgdmxList.get(i).get("rksl"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("hsj")) {
                    sbTable.append(df.format(cgdmxList.get(i).get("hsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgdmxList.get(i).get("hsj")&&null != cgdmxList.get(i).get("rksl")) {
                    Double myshje = ((Double) cgdmxList.get(i).get("hsj")) * ((Double) cgdmxList.get(i).get("rksl"));
                    BigDecimal byshje = new BigDecimal(myshje);
                    Double dyshje = byshje.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(jtdf.format(dyshje));
                    yshje += dyshje;

//                    sbTable.append(((Double)cgdmxList.get(i).get("hsj"))*((Double)cgdmxList.get(i).get("rksl")));
//                    yshje += ((Double)cgdmxList.get(i).get("hsj"))*((Double)cgdmxList.get(i).get("rksl"));
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"6\">合计");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(sbsl);
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(rkjs);
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(yshje);
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</table>");
        }
        renderHtml(sbTable.toString());
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getCgdId() {
        return cgdId;
    }

    public void setCgdId(Long cgdId) {
        this.cgdId = cgdId;
    }

    public Double getCe() {
        return ce;
    }

    public void setCe(Double ce) {
        this.ce = ce;
    }

    public int getIndex() {
        return index;
    }

    public String getZdr() {
        return zdr;
    }

    public void setZdr(String zdr) {
        this.zdr = zdr;
    }

    public void setIndex(int index) {
        this.index = index;
    }
    @Autowired
    public void setNbcgdService(NbcgdService nbcgdService) {
        this.nbcgdService = nbcgdService;
    }

    public QueryCgd getQueryCgd() {
        return queryCgd;
    }

    public void setQueryCgd(QueryCgd queryCgd) {
        this.queryCgd = queryCgd;
    }

    public Date getSysdate() {
        return sysdate;
    }

    public void setSysdate(Date sysdate) {
        this.sysdate = sysdate;
    }

    public Date getDatetime() {
        return datetime;
    }

    public void setDatetime(Date datetime) {
        this.datetime = datetime;
    }

    public Date getFhrqStart() {
        return fhrqStart;
    }

    public void setFhrqStart(Date fhrqStart) {
        this.fhrqStart = fhrqStart;
    }

    public Date getFhrqEnd() {
        return fhrqEnd;
    }

    public void setFhrqEnd(Date fhrqEnd) {
        this.fhrqEnd = fhrqEnd;
    }

    public Long getCgjsdId() {
        return cgjsdId;
    }

    public void setCgjsdId(Long cgjsdId) {
        this.cgjsdId = cgjsdId;
    }

    public String getCgdh() {
        return cgdh;
    }

    public void setCgdh(String cgdh) {
        this.cgdh = cgdh;
    }

    public Long getKhId() {
        return khId;
    }

    public Long getCgdwId() {
        return cgdwId;
    }

    public void setCgdwId(Long cgdwId) {
        this.cgdwId = cgdwId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }

    public Long getCggsId() {
        return cggsId;
    }

    public void setCggsId(Long cggsId) {
        this.cggsId = cggsId;
    }

    public Long getCgbmId() {
        return cgbmId;
    }

    public Date getCurrentTime() {
        return CurrentTime;
    }

    public void setCurrentTime(Date currentTime) {
        CurrentTime = currentTime;
    }

    public void setCgbmId(Long cgbmId) {
        this.cgbmId = cgbmId;
    }

    public Long getCgrId() {
        return cgrId;
    }

    public void setCgrId(Long cgrId) {
        this.cgrId = cgrId;
    }

    public Long getGcId() {
        return gcId;
    }

    public void setGcId(Long gcId) {
        this.gcId = gcId;
    }

    public Long getCghtId() {
        return cghtId;
    }

    public void setCghtId(Long cghtId) {
        this.cghtId = cghtId;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public Cgd getCgd() {
        return cgd;
    }

    public void setCgd(Cgd cgd) {
        this.cgd = cgd;
    }

    public Long getGysid() {
        return gysid;
    }


    public void setGysid(Long gysid) {
        this.gysid = gysid;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public String getCglx() {
        return cglx;
    }

    public void setCglx(String cglx) {
        this.cglx = cglx;
    }

    public String getEwmtime() {
        return ewmtime;
    }

    public void setEwmtime(String ewmtime) {
        this.ewmtime = ewmtime;
    }

    public String getSftzd() {
        return sftzd;
    }

    public void setSftzd(String sftzd) {
        this.sftzd = sftzd;
    }

    public String getHtType() {
        return htType;
    }

    public void setHtType(String htType) {
        this.htType = htType;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }
}
